package com.charlotte.gupao.study.springbootrabbitmqdemo.delay;

import com.charlotte.gupao.study.springbootrabbitmqdemo.config.DelayQueueConfig;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author junjie.ding
 * @date 2020/10/30 2:28
 * @description TODO
 */
@Component
public class DelayedMsgProvider {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMsg(String msg, long delayTime){

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setHeader("x-delay", delayTime);
        Message message = new Message(msg.getBytes(), messageProperties);

        System.out.println("发送延迟队列时间：" + System.currentTimeMillis());
        rabbitTemplate.send(DelayQueueConfig.DELAYED_EXCHANGE_NAME, DelayQueueConfig.DELAYED_QUEUE_NAME, message);

//        amqpTemplate.convertAndSend(DelayQueueConfig.DELAYED_EXCHANGE_NAME, DelayQueueConfig.DELAYED_QUEUE_NAME, msg, message -> {
//            System.out.println("发送延迟队列时间：" + System.currentTimeMillis());
//            message.getMessageProperties().setHeader("x-delay", delayTime); // 设置延迟投递时间
//            return message;
//        });
    }
}
